package example.com.widgetdemo;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements NormalRecyclerViewAdapter.OnItemClickLitener{

    private ArrayList<BGEntity> bgEntities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText et_motto = (EditText) findViewById(R.id.et_motto);
        et_motto.setText(Utility.getMottoText(MainActivity.this));
        Button btn_save = (Button) findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String motto = et_motto.getText().toString();
                Utility.saveMottoText(MainActivity.this, motto);
                updateWidget(motto);
            }
        });

        bgEntities = new ArrayList<>();
        bgEntities.add(new BGEntity(R.drawable.bg_muban, "裂痕木纹背景"));
        bgEntities.add(new BGEntity(R.drawable.bg_red, "红色科技感背景"));
        bgEntities.add(new BGEntity(R.drawable.bg_zhuzi, "竹子背景"));
        bgEntities.add(new BGEntity(R.drawable.bg_xingkong, "星空背景"));
        bgEntities.add(new BGEntity(R.drawable.bg_black, "黑色背景"));
        bgEntities.add(new BGEntity(R.drawable.bg_touming, "透明背景"));


        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.list_bg);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//这里用线性宫格显示 类似于grid view
        NormalRecyclerViewAdapter adapter = new NormalRecyclerViewAdapter(this, bgEntities);
        adapter.setOnItemClickLitener(this);
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    public void onItemClick(View view, int position) {
        updateWidget(bgEntities.get(position).getDrawableResource());
        Utility.saveMottoBg(MainActivity.this, bgEntities.get(position).getDrawableResource());
        Toast.makeText(this, "背景图片已换成" + bgEntities.get(position).getDescribe(), Toast.LENGTH_SHORT).show();
    }

    private void updateWidget(String motto) {
        // 更新 Widget
        RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget_attach);
        remoteViews.setTextViewText(R.id.tv_motto, motto);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        appWidgetManager.updateAppWidget(new ComponentName(this, MottoWidget.class), remoteViews);
    }

    private void updateWidget(int drawableRes) {
        // 更新 Widget
        RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget_attach);
        remoteViews.setImageViewResource(R.id.bg_root, drawableRes);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        appWidgetManager.updateAppWidget(new ComponentName(this, MottoWidget.class), remoteViews);
    }
}
